<template>
  <el-card class="box-card" style="margin-top: 10px">
    <div slot="header" class="clearfix">
      <!--  @tab-click="handleClick" -->
      <!-- 左侧内容 -->
      <el-tabs class="tab" v-model="activeName">
        <el-tab-pane label="销售额" name="seal"></el-tab-pane>
        <el-tab-pane label="访问量" name="visits"></el-tab-pane>
      </el-tabs>
      <!-- 右侧内容 -->
      <div class="right">
        <span @click="setDay">今日</span>
        <span @click="setWeek">本周</span>
        <span @click="setMonth">本月</span>
        <span @click="setYear">本年</span>
        <!--    v-model="value1"  -->
        <!-- <el-date-picker
          v-model="date"
          class="date"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          size="mini"
          value-format="yyyy-mm-dd"
        >
        </el-date-picker> -->
        <el-date-picker
        v-model="date"
          class="date"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </div>
    </div>
    <!-- 内容主体 -->
    <div>
      <el-row :gutter="10">
        <el-col :span="18">
          <!-- <div>销售额趋势</div> -->
          <div ref="charts" class="charts"></div>
        </el-col>
        <el-col :span="6">
          <div class="rvalue">门店{{ title }}排行</div>
          <ul>
            <li>
              <span class="rindex">1</span>
              <span class="">肯德基</span>
              <span class="">323,234</span>
            </li>
            <li>
              <span class="rindex">2</span>
              <span class="">麦当劳</span>
              <span class="">323,234</span>
            </li>
            <li>
              <span class="rindex">3</span>
              <span class="">海底捞</span>
              <span class="">323,234</span>
            </li>
            <li>
              <span class="rindexs">4</span>

              <span class="">瑞幸咖啡</span>
              <span class="">323,234</span>
            </li>
            <li>
              <span class="rindexs">5</span>

              <span class="">巴奴火锅</span>
              <span class="">323,234</span>
            </li>
            <li>
              <span class="rindexs">6</span>

              <span class="">小放牛</span>
              <span class="">323,234</span>
            </li>
            <li>
              <span class="rindexs">7</span>

              <span class=""> 塔斯汀 </span>
              <span class="">323,234</span>
            </li>
          </ul>
        </el-col>
      </el-row>
    </div>
  </el-card>
</template>

<script>
import * as echarts from "echarts";
import dayjs from "dayjs";
export default {
  data() {
    return {
      // 展示默认tab项
      activeName: "seal",
      myChart: null,
      date: [],
    };
  },
  // 这里用的是本地的json数据 并未使用mock 【其实是由bug解决不了 没办法了】
  props: ["echartsList"],
  mounted() {
    this.myChart = echarts.init(this.$refs.charts);
    this.myChart.setOption({
      title: {
        text: this.title + "趋势",
        left: "left",
        top: "left",
        textStyle: {
          fontSize: 16,
        },
      },
      tooltip: {
        trigger: "axis",
        axisPointer: {
          type: "shadow",
        },
      },
      grid: {
        left: "3%",
        right: "4%",
        bottom: "3%",
        containLabel: true,
      },
      xAxis: [
        {
          type: "category",
          data: [],
          axisTick: {
            alignWithLabel: true,
          },
        },
      ],
      yAxis: [
        {
          type: "value",
        },
      ],
      series: [
        {
          name: "Direct",
          type: "bar",
          barWidth: "60%",
          data: this.List.orderFullYear,
          color: "yellowgreen",
        },
      ],
    });
  },
  computed: {
    title() {
      //根据用户点击计算出动态title值
      return this.activeName == "seal" ? "销售额" : "访问量";
    },
    // 计算出props传过来的图表数据
    List() {
      return this.$props.echartsList;
    },
  },
  watch: {
    title() {
      // 既然你需要使用watch监听 那么你就得把要监听的数据配置在data中 才可以拿到并且操作不是吗？
      this.myChart.setOption({
        // 当监听到图表的title发生变化后 将其赋值为最新值 展示到页面中
        title: {
          text: this.title + "趋势",
        },
        xAxis: {
          data:
            this.title == "销售额"
              ? this.List.orderFullYearAxis
              : this.List.userFullYearAxis,
        },
        series: [
          {
            name: "Direct",
            type: "bar",
            barWidth: "60%",
            data:
              this.title == "销售额"
                ? this.List.orderFullYear
                : this.List.userFullYear,
            color: "yellowgreen",
          },
        ],
      });
    },
    // 同理 因mounted只执行一次 所以需要监听List的图表保证数据真的计算出来了 在进行默认展示
    List() {
      this.myChart.setOption({
        xAxis: {
          data: this.List.orderFullYearAxis,
        },
        series: [
          {
            data: this.List.orderFullYear,
          },
        ],
      });
    },
  },
  methods: {
    //本天
    setDay() {
      const day = dayjs().format("YYYY-MM-DD");
      this.date = [day, day];
    },
    //本周
    setWeek() {
      const start = dayjs().day(1).format("YYYY-MM-DD");
      const end = dayjs().day(7).format("YYYY-MM-DD");
      this.date = [start, end];
    },
    //本月
    setMonth() {
      const start = dayjs().startOf("month").format("YYYY-MM-DD");
      const end = dayjs().endOf("month").format("YYYY-MM-DD");
      this.date = [start, end];
    },
    //本年
    setYear() {
      const start = dayjs().startOf("year").format("YYYY-MM-DD");
      const end = dayjs().endOf("year").format("YYYY-MM-DD");
      this.date = [start, end];
    },
  },
};
</script>

<style scoped>
.clearfix {
  position: relative;
  display: flex;
  justify-content: space-between;
}
.tab {
  width: 100%;
}
.right {
  position: absolute;
  right: 0;
}
.right span {
  padding: 0 8px;
}
.date {
  width: 249px;
  margin: 0 10px;
}
.charts {
  width: 100%;
  height: 300px;
}
.rvalue {
  font-size: 14px;
}
ul {
  width: 100%;
  height: 300px;
  padding: 0;
}
ul li {
  list-style: none;
  height: 8%;
  margin: 15px 0;
}
li span {
  margin: 0 10px;
  font-size: 14px;
}

.rindex {
  float: left;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  color: #fff;
  background-color: black;
  text-align: center;
}
.rindexs {
  float: left;
  width: 20px;
  height: 20px;
  color: #ccc;
  text-align: center;
}
</style>